#include "profiler.h"

#include <Windows.h>
#include <fstream>

std::ofstream g_File;
unsigned int g_uiLevel = 0;
double			g_Frequency=0;
double			g_StartTime=0;
void ProfilerInit(const char *szFileName)
{
	g_File.open(szFileName, std::ios::beg);
	__int64	i64Frequency;
	__int64	i64StartTime;
	QueryPerformanceFrequency((LARGE_INTEGER*)&i64Frequency);
	QueryPerformanceCounter((LARGE_INTEGER*)&i64StartTime);
	g_Frequency = i64Frequency;
	g_StartTime = i64StartTime;
}
SpeedTester::SpeedTester(const char *szFunction)
{
	m_szFuncName = szFunction;
	for(unsigned int ui=0; ui<g_uiLevel+2; ui++)
		g_File<<"\t";
	g_uiLevel++;
	g_File<<m_szFuncName<<std::endl;

	
	QueryPerformanceCounter((LARGE_INTEGER*)&m_i64LastTime);
}

SpeedTester::~SpeedTester()
{
	static double DiffTime = 0;
	static __int64 i64EndTime = 0;
	QueryPerformanceCounter((LARGE_INTEGER*)&i64EndTime);

	DiffTime = i64EndTime - m_i64LastTime;
	DiffTime *= (1000000.0/g_Frequency);
	g_uiLevel--;

	g_File<<(int)(1000000.0*(i64EndTime - g_StartTime)/g_Frequency)<<"\t";
	g_File<<(int)DiffTime;
	for(unsigned int ui=0; ui<g_uiLevel+1; ui++)
		g_File<<"\t";
	g_File<<m_szFuncName<<std::endl;
}